package com.gjy.monitor.controller;

import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.exporter.PushGateway;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-02-15 10:27:34
 */
@RestController
@RequestMapping("/pushgateway")
public class PushGatewayController {

    @Resource
    private PushGateway pushGateway;
    Gauge gauge = Gauge.build().name("test_gauge").help("test gauge").register();

    @RequestMapping("/gauge")
    public String gauge(@RequestParam Double value) throws IOException {
        gauge.set(value);
        pushGateway.pushAdd(gauge, "gauge-job");
        return "test";
    }

    Counter counter = Counter.build()
            .name("test_counter")
            .help("test counter")
            .labelNames("label1", "label2")
            .register();

    @RequestMapping("/counter")
    public String counter() throws IOException {
        pushGateway.pushAdd(counter, "my-job");
        return "test";
    }
}
